<%--                                                                       --%>
<%-- Copyright (c) 2005 BroadVision, Inc.  All rights reserved.            --%>
<%--                                                                       --%>
<%-- This software is copyrighted. Under the copyright laws, this software --%>
<%-- may not be copied, in whole or in part, without prior written consent --%>
<%-- of BroadVision, Inc. or its assignees. This software is provided      --%>
<%-- under the terms of a license between BroadVision and the recipient,   --%>
<%-- and its use is subject to the terms of that license.                  --%>
<%--                                                                       --%>
<%-- This software may be protected by one or more U.S. and International  --%>
<%-- patents.  Certain applications of BroadVision One-To-One software are --%>
<%-- covered by U.S. patent 5,710,887.                                     --%>
<%--                                                                       --%>
<%-- TRADEMARKS: BroadVision and BroadVision One-To-One are registered     --%>
<%-- trademarks of BroadVision, Inc., in the United States and the         --%>
<%-- European Community, and are trademarks of BroadVision, Inc., in other --%>
<%-- countries.  The BroadVision logo, is a trademark of BroadVision,      --%>
<%-- Inc., in the United States and other countries. Additionally, IONA    --%>
<%-- and Orbix are trademarks of IONA Technologies, Ltd.  RSA, MD5, and    --%>
<%-- RC2 are trademarks of RSA Data Security, Inc.                         --%>
<%--                                                                       --%>

<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/struts-html-el.tld" prefix="html-el" %>
<%@ taglib uri="/WEB-INF/struts-bean-el.tld" prefix="bean-el" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>

<%
  String ctxPath = request.getContextPath();
%>

<script language="JavaScript">

function doCancel(form) {
  form.action = "<c:url value="/ep/userTemplatePagesView.do"/>";
  form.submit();
}

function checkNumberOnly(e) {
  var charCode = (navigator.appName == "Netscape") ? e.which : e.keyCode;
  if (charCode > 31 && (charCode < 48 || charCode > 57)) {
    return false;
  }
  else {
    return true;
  }
}

function findField(theForm, fieldName) {
  var elements = theForm.elements;
  var numElements = elements.length;
  var returnElement = null;
  for (var elementIndex = 0; elementIndex < numElements; elementIndex++) {
    var elementName = elements[elementIndex].name;
    if (elementName == fieldName) {
      returnElement = elements[elementIndex];
      break;
    }
  }
  return returnElement;
}

function doRequired(form, orderField) {
  var field = findField(form, orderField);
  if (field != null) {
    field.style.visibility = 'visible';
    field.style.display = '';
  }
  return true;
}

function doDefault(form, orderField) {
  var field = findField(form, orderField);
  if (field != null) {
    field.style.visibility = 'visible';
    field.style.display = '';
  }
  return true;
}

function doOptional(form, orderField) {
  var field = findField(form, orderField);
  if (field != null) {
    field.style.visibility = 'hidden';
    field.style.display = 'none';
  }
  return true;
}

function doValidation(form) {
  var maxPageNumber = <c:out value="${epUserTemplatePagesView.map.maxPageNumber}"/>;
  if (maxPageNumber <= 0)
    return true;
  var countRequired = 0;
  var fieldName = '';
  var field = null;
<c:forEach var="page" items="${epUserTemplatePagesView.map.pages}" varStatus="index">
  fieldName = 'type_' + '<c:out value="${page.map.id}"/>' + '[0]';
  eval('var resultChecked = form.' + fieldName + '.checked');
  eval('var resultValue = form.' + fieldName + '.value');
  if (resultChecked != null && resultChecked == true && resultValue != null && resultValue == 2) {
    countRequired++;
  }
</c:forEach>
  if (maxPageNumber < countRequired) {
    alert('<bean-el:message key="error.user.template.exceedMaxPageNumber" bundle="ep.messages" arg0="${epUserTemplatePagesView.map.maxPageNumber}"/>');
    return false;
  }
  return true;
}

function doResetAction(form) {
  form.reset();
<c:forEach var="page" items="${epUserTemplatePagesView.map.pages}" varStatus="index">
  <c:if test="${page.map.type == 2}">doRequired(form, 'order_<c:out value="${page.map.id}"/>');</c:if>
  <c:if test="${page.map.type == 1}">doDefault(form, 'order_<c:out value="${page.map.id}"/>');</c:if>
  <c:if test="${page.map.type == 0}">doOptional(form, 'order_<c:out value="${page.map.id}"/>');</c:if>
</c:forEach>
}

<%@ include file="/mc/common/jsutils.jsp" %>

</script>

<%@include file="/mt/common/pickerPopupHelper.jsp" %>

<table class="zoneFive" width="100%" border="0" cellspacing="0" cellpadding="0" summary="content area">
  <tr>
    <td valign="top" width="100%">
      <div class="pageTitle">
        <bean-el:message key="ep.user.template.edit.page.title" bundle="ep.messages" arg0="${epUserTemplatePagesView.map.name}"/>
      </div>
      <div class="errorMessage"><html-el:errors/></div>
      <%@ include file="/mc/common/pagetab.jsp" %>

      <html-el:form action="/ep/userTemplatePagesSave.do">
        <input type="hidden" name="userTemplateId" value="<c:out value="${epUserTemplatePagesView.map.id}"/>"/>
        <div class="pageSubtitle">
          <span class="left"><bean-el:message key="ep.user.template.page.types.selected" bundle="ep.messages"/></span>
          <span class="right">&nbsp;</span>
        </div>
        <select name="pageTypeIds" multiple="multiple" size="0" style="visibility: hidden; display: none;"></select>
      </html-el:form>

      <html-el:form action="/ep/userTemplatePagesOrderSave.do" onsubmit="return doValidation(epUserTemplatePagesOrderForm);">
        <input type="hidden" name="userTemplateId" value="<c:out value="${epUserTemplatePagesView.map.id}"/>"/>
        <table class="dataTable" border="0" cellspacing="1" cellpadding="2" summary="Search result table" width="100%">
          <tr class="listHeader">
            <th id="Name" align="left" width="40%"><bean-el:message key="ep.header.name" bundle="ep.messages"/></th>
            <th id="Description" align="left" width="40%"><bean-el:message key="ep.header.description" bundle="ep.messages"/></th>
            <th id="Order" align="center" width="5%"><bean-el:message key="ep.header.order" bundle="ep.messages"/></th>
            <th id="Required" align="center" width="5%"><bean-el:message key="ep.header.required" bundle="ep.messages"/></th>
            <th id="Default" align="center" width="7%"><bean-el:message key="ep.header.default" bundle="ep.messages"/></th>
            <th id="Optional" align="center" width="7%"><bean-el:message key="ep.header.optional" bundle="ep.messages"/></th>
          </tr>
<c:choose>
  <c:when test="${empty epUserTemplatePagesView.map.pages}">
          <tr class="listOdd">
            <td align="center" colspan="6">
              <bean-el:message key="ep.user.template.page.types.noFound" bundle="ep.messages"/>
            </td>
          </tr>
  </c:when>
  <c:otherwise>
    <c:forEach var="page" items="${epUserTemplatePagesView.map.pages}" varStatus="index">
      <c:choose>
        <c:when test="${(index.count + 1) % 2 == 0}">
          <tr class="listEven">
        </c:when>
        <c:otherwise>
          <tr class="listOdd">
        </c:otherwise>
      </c:choose>
            <input type="hidden" name="pageIds" value="<c:out value="${page.map.id}"/>"/>
            <td headers="Name"><c:out value="${page.map.name}"/></td>
            <td headers="Description"><c:out value="${page.map.description}"/></td>
            <td headers="Order" align="center">
              <input type="text" name="order_<c:out value="${page.map.id}"/>"
                <c:if test="${page.map.type == 0}">style="visibility: hidden; display: none;"</c:if>
                size="2" maxlength="2" onkeypress="return checkNumberOnly(event)"
                value="<c:if test="${page.map.order != -1}"><c:out value="${page.map.order}"/></c:if>"/>
            </td>
            <td headers="Required" align="center">
              <input type="radio" name="type_<c:out value="${page.map.id}"/>" value="2"
                onclick="return doRequired(this.form, 'order_<c:out value="${page.map.id}"/>');"
                <c:if test="${page.map.type == 2}">checked="checked"</c:if>/>
            </td>
            <td headers="Default" align="center">
              <input type="radio" name="type_<c:out value="${page.map.id}"/>" value="1"
                onclick="return doDefault(this.form, 'order_<c:out value="${page.map.id}"/>');"
                <c:if test="${page.map.type == 1}">checked="checked"</c:if>/>
            </td>
            <td headers="Optional" align="center">
              <input type="radio" name="type_<c:out value="${page.map.id}"/>" value="0"
                onclick="return doOptional(this.form, 'order_<c:out value="${page.map.id}"/>');"
                <c:if test="${page.map.type == 0}">checked="checked"</c:if>/>
            </td>
          </tr>
    </c:forEach>
  </c:otherwise>
</c:choose>
        </table>
<c:if test="${not empty epUserTemplatePagesView.map.pages}">
        <div class="pageButtonWrap" align="right">
          <input type="button" name="resetBtn" class="pageButton"
            onclick="doResetAction(this.form);" value="<bean-el:message key='button.reset' bundle="ep.messages"/>">
          &nbsp;&nbsp;
          <input type="button" name="actionCancel" class="pageButton"
            onclick="doCancel(this.form);" value='<bean-el:message key="button.cancel" bundle="ep.messages"/>'>
          <input type="submit" name="saveData" class="pageButton" value="<bean-el:message key='button.save' bundle="ep.messages"/>">
        </div>
</c:if>
      </html-el:form>
    </td>
  </tr>
</table>
